package com.zou.szt;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.zou.szt.Utils.GetCards;
import com.zou.szt.Utils.JsonUtils;
import com.zou.szt.Utils.ToastUtil;
import com.zou.szt.Utils.VolleySingleton;

/**
 * Created by zou on 2016/5/19.
 */
public class AddDialogFragment extends BaseDialogFragment {


    OnSubmitListener listener;


    @Override
    public void onAttach(Activity activity) {
//        添加Fragment时调用，在其中初始化listener。
        super.onAttach(activity);
        listener = (OnSubmitListener) activity;
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        LayoutInflater mInflater = LayoutInflater.from(getActivity());
        View view = mInflater.inflate(R.layout.dialog_add, null);
        final RequestQueue requestQueue = VolleySingleton.getVolleySingleton(getContext()).getRequestQueue();


        final EditText editText = (EditText) view.findViewById(R.id.edt);
        final Button yesbtn = (Button) view.findViewById(R.id.btn_yes);
        Button nobtn = (Button) view.findViewById(R.id.btn_no);
        final ProgressBar pgb = (ProgressBar) view.findViewById(R.id.pgb);
        pgb.setVisibility(View.GONE);


        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setView(view).setTitle("添加深圳通");


        nobtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });


        yesbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s = editText.getText().toString();
                if (s.length() < 9) {
                    Toast.makeText(getContext(), "请输入9位深圳通卡号", Toast.LENGTH_SHORT).show();
                    editText.startAnimation(AnimationUtils.loadAnimation(getContext(), android.R.anim.fade_in));
                } else if (GetCards.requestCards(getContext(), s)) {
//                    卡号已存在
                    Toast.makeText(getContext(), "已添加过此卡！", Toast.LENGTH_SHORT).show();
                    editText.startAnimation(AnimationUtils.loadAnimation(getContext(), android.R.anim.fade_in));
                } else {
                    StringRequest stringRequest = new StringRequest(Request.Method.GET,
                            Constants.URL + editText.getText(),
                            new Response.Listener<String>() {

                                @Override
                                public void onResponse(String response) {
                                    try {
                                        ResultBean data = (ResultBean) JsonUtils.fromJson(response, ResultBean.class);
                                        //                                        验证号码
                                        if (!TextUtils.equals(data.getCard_number(), "0")) {
                                            // 判断卡号
                                            GetCards.putCards(getContext(), editText.getText().toString());
                                            ToastUtil.showToast(getContext(), "添加成功");
                                            listener.sendMessage("whatever");
                                            dismiss();
                                        } else {
                                            ToastUtil.showToast(getContext(), "查询失败，卡号有误或服务异常");
                                            editText.startAnimation(AnimationUtils.loadAnimation(getContext(), android.R.anim.fade_in));
                                            pgb.setVisibility(View.GONE);
                                            yesbtn.setClickable(true);
                                        }
                                    } catch (Exception e) {
                                        e.printStackTrace();
                                        ToastUtil.showToast(getContext(), "查询失败");
                                        pgb.setVisibility(View.GONE);
                                        yesbtn.setClickable(true);

                                    }
                                }
                            },
                            new Response.ErrorListener() {
                                @Override
                                public void onErrorResponse(VolleyError error) {
                                    ToastUtil.showToast(getContext(), "查询失败");

                                }
                            });
                    stringRequest.setShouldCache(false);
                    requestQueue.add(stringRequest);
                    pgb.setVisibility(View.VISIBLE);
                    yesbtn.setClickable(false);
                }
            }
        });

        return builder.create();
    }
}
